﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Collections;

namespace FooFun.DataElements.Dtos
{
    public class XmlNodeListDataElement : DataElement
    {
        public override string ToString()
        {
            return this.Value.ToString();
        }


        XmlNodeList list;
        public XmlNodeListDataElement(XmlNodeList list)
        {
            this.list = list;
        }

        #region IDataElement 成员

        public override object Value
        {
            get
            {
                string[] strs = new string[list.Count];
                for (int i = 0; i < list.Count; i++)
                {
                    strs[i] = list[i].InnerXml;
                }

                return strs;
            }
            set
            {
                base.Value = value;
            }
        }

        public override IDataElement this[string attrName]
        {
            get
            {
                XmlNode firstNode = this.list[0];
                XmlNodeList newList = firstNode.ParentNode.SelectNodes(firstNode.Name + "/attrName");

                return new XmlNodeListDataElement(newList);
            }
            set
            {
                foreach (XmlNode node in this.list)
                {
                    if (node.Name == attrName)
                    {
                        node.InnerXml = value.Value.ToString();
                    }
                }
            }
        }

        public override IDataElement Add(IDataElement de)
        {
            XmlElementDataElement nodeEle = (XmlElementDataElement)de;

            this.list[0].ParentNode.AppendChild(nodeEle.Node);

            return nodeEle;
        }

        public override bool IsEmpty
        {
            get
            {
                return list.Count == 0;
            }
        }


        #endregion

        #region IEnumerable 成员

        public override IEnumerator GetEnumerator()
        {
            foreach (XmlElement node in this.list)
            {
                yield return new XmlElementDataElement(node);
            }
        }

        #endregion
    }
}
